import { Component, OnInit, ChangeDetectorRef, ViewChild, Inject, forwardRef } from '@angular/core';
import { ImageManagementEditGridComponent } from './select-image-edit-grid/select-image-edit-grid.component';
import { path } from '../../../../../../path.config';
import { masterService } from '../../../../../../../lib/master.service';
import { saveVarService } from '../../../../../../../lib/saveVar.service';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { AppComponent } from '../../../../../../app.component';

@Component({
    selector: 'app-select-image-edit',
    templateUrl: './select-image-edit.component.html',
    styleUrls: ['./select-image-edit.component.css'],
    providers: [masterService, forwardRef(() => AppComponent)]
})
export class SelectImageEditComponent implements OnInit {
    @ViewChild('selectImageGrid') public selectImageGrid: ImageManagementEditGridComponent;
    @ViewChild('selectImageEditGridModal') public selectImageEditGridModal: ModalDirective;
    // 获取列表项api
    private getList = path.materialManagement.imageManagement.getImgList;
    // 搜索传参的对象
    public filter: any = {
        imageName: '',
        pageSize: '10',
        pageIndex: '1'
    }

    constructor(private _masterService: masterService,
        private cdRef: ChangeDetectorRef,
        public _saveVar: saveVarService,
        @Inject(forwardRef(() => AppComponent)) private app: AppComponent) {
    }

    public ngOnInit() { }

    // 过滤grid数据
    public selectListItem() {
        this._masterService
            .getData(this.getList, this.filter)
            .subscribe((res) => {
                if (res.data) {
                    this.selectImageGrid.rows = res.data.items;
                    this.selectImageGrid.totalItems = res.data.pagination.totalCount;
                    this.selectImageGrid.page = parseInt(res.data.pagination.pageIndex);
                    this.selectImageGrid.itemsPerPage = res.data.pagination.pageSize;
                    this.selectImageGrid.cdRef.detectChanges();
                }
            })
    }

    public openEditModal() {
        this.selectImageEditGridModal.show();
    }

    public closeAddModal(Id) {
        this.selectImageEditGridModal.hide();
        this._saveVar.threeAdEditPicId = Id;
        this.app.toastSuccess('更改成功！');
    }
}
